﻿// JScript 文件
var m_idx = 0;
var m_bmlx = null;
var m_bm   = null;
var m_params = null;
var m_xmlDoc = null;    //存放界面XML对象
var m_btnView = null; //按钮视图
var m_arrTab = new Array(); //存放TabCtrl
var m_arrList = new Array();    //存放ListCtrl
var m_activeList = null;


var AssViewStyle = {
    Null      : 0       //未定义
    ,PrjMgr    : 1      //工程管理工具
    ,AssPrj    : 2      //工程转固流程
    ,AssExt    : 3      //资产盘点流程
    ,AssLg     : 4      //零购转固流程
    ,AssPrj2   : 5      //工程转固流程2
    ,AssExt2   : 6      //资产盘点流程2
    ,AssXz     : 7      //卡片闲置流程
    ,AssDb     : 8      //卡片调拨流程            
    ,AssReduce : 9      //卡片减少流程
    ,AssZkpMgr :10      //子卡片管理
    ,AssZkpChg :11      //子卡片变更管理
    ,AssFkpMgr :12      //父卡片管理
    ,AssFkpChg :13      //父卡片变更管理
    ,AssLocal  :14      //本地网资产实体管理
    ,AssTmp01  :15      //单列表框模板
    ,AssTmp10  :16      //单导航框模板
    ,AssTmp11  :17      //1导航框1列表框模板
    ,AssTmp12  :18      //1导航框2列表框模板
    ,AssView01 :19      //ListView
    ,AssView10 :20      //TreeBarView
    ,AssView11 :21      //ButtonView
    ,AssView12 :22      //StaticEditView
}
var AssUiNodeKind = {
    NULL      : 0      //未定义
    , Menu     : 1  //框架菜单
    , RMenu    : 2  //右键菜单
    , Dialog   : 3  //对话框
    , Tab      : 4  //TAB框架
    , Tree     : 5  //Tree页面
    , List     : 6  //List页面
    , Button   : 7  //按钮
    , ListFld  : 8  //list控件字段
    , JkTab    : 9  //接口表
    , JkTabFld :10  //接口表字段
    , Static   :11  //标注
    , Edit     :12  //Edit框
}
var AssUiAttStyle = 
{
    Null : 0        //未定义
  ,Prj : 1        //工程列表
  ,Prj_Fkp : 2        //工程卡片列表
  ,Zkp : 3        //资产实体列表
  ,Zkp_Chg : 4        //资产变更列表
  ,Fkp : 5        //卡片列表
  ,Fkp_Chg : 6        //卡片变更列表
  ,ZyGdzcdz : 7       //资源固定资产对照列表
  ,Ext_Fkp : 8       //固定资产盘点列表        
}

//Tab响应事件函数
function onTab(list)
{
    //如果没加载过
    if(list.arrCol.length < 1)
    {
        list.goPage(1);
    }
    else
    {
        list.resetFlow();
    }
    if (m_btnView && list != m_activeList)
    {
        m_activeList = list;
        createButtonView(m_activeList);
    }
}

//初始化
function init()
{
    m_params = window.location.href.toQueryParams();
    m_idx = parseInt(m_params["idx"]);//流程功能点
    m_bmlx = m_params["bmlx"];//编码类型jzbm,gcbh
    m_bm = m_params["bm"];//编码
    //如果不是向导式
    if (m_idx != m_vt.ASS_MAIN)
    {
        m_xmlDoc = CtnTrans.getXml("/main.ashx?_Method=GetAssMainCfg", "POST", "idx=" + m_idx);
    }
    else
    {
        m_xmlDoc = CtnTrans.getXml("/main.ashx?_Method=GetAssMainCfg", "POST", "id0=" + parseInt(m_params["id0"]));
    }
    if(!CtnError.validate(m_xmlDoc))
    {
        CtnError.show();
        return;
    }
    var xmlRoot = CtnXML.getRootNode(m_xmlDoc);//
    for (var i = 0; i < xmlRoot.childNodes.length; i++)
    {
        var xmlView = xmlRoot.childNodes[i];
        if ($XML(xmlView, "kind") != AssUiNodeKind.Dialog)
        {
            continue ;
        }
        var elemView = document.createElement("div");//创建视图
        //elemView.object = xmlView;
        elemView.style.width = "100%";
        elemView.style.margin = "1px 2px";
        elemView.style.border = "1px solid #454545";  
        document.body.appendChild(elemView);
        if ($XML(xmlView, "style") == AssViewStyle.AssView01)
        {
            //elemView.style.height = "100px";
            createListView(elemView, xmlView);
        }
        else if ($XML(xmlView, "style") == AssViewStyle.AssView10)
        {
            //createTreeBarView(elemView, xmlView);
        }
        else if ($XML(xmlView, "style") == AssViewStyle.AssView11)
        {
            m_btnView = elemView;
            elemView.style.height = "46px";//按钮视图和统计视图直接给高度
            //createButtonView(elemView, xmlView);
        }
        else if ($XML(xmlView, "style") == AssViewStyle.AssView12)
        {
            //elemView.style.height = "50px";//按钮视图和统计视图直接给高度
            //createStaticEditView(elemView, xmlView);
            elemView.parentNode.removeChild(elemView);
        }
    }
    if (m_arrList.length > 0)
    {
        onTab(m_arrList[0]);
    }
    initFrame();
}
//
function getDivHeight()
{
    var root = m_xmlDoc.documentElement;
    var count = 0;
    for (var i = 0; i < root.childNodes.length; i++)
    {
        if ($XML(root.childNodes[i], "kind") == AssUiNodeKind.Dialog
            && $XML(root.childNodes[i], "style") == AssViewStyle.AssView01)
        {
            count++;
        }
    }
    if (count > 0)
    {
        return (document.documentElement.clientHeight - 75) / count;
    }
    else
    {
        return 0;
    }
}
function getRowCount()
{
    var root = m_xmlDoc.documentElement;
    var count = 0;
    for (var i = 0; i < root.childNodes.length; i++)
    {
        if ($XML(root.childNodes[i], "kind") == AssUiNodeKind.Dialog
            && $XML(root.childNodes[i], "style") == AssViewStyle.AssView01)
        {
            count++;
        }
    }
    if (count == 1)
    {
        return 16;
    }
    else if(count == 2)
    {
        return 6;
    }
    else
    {
        return 5;
    }
}
//初始化布局
function initFrame()
{
    var docHeight = document.documentElement.clientHeight;
    //var lstTab = $(document).getElementsByClassName("head_td_back");
    //减去tab和btn的高度，还剩下list的总共高度
    var resHeight = docHeight - (m_btnView?m_btnView.offsetHeight:0) - m_arrTab.length * 29;
    var lstList = []; //展开list个数
    for (var i = 0; i < m_arrTab.length; i++)
    {
        if(m_arrTab[i].nextSibling.style.display != "none")
        {
            lstList.push(m_arrTab[i].nextSibling);
        }
    }
    if (lstList.length > 0)
    {
        for (var i = 0; i < lstList.length; i++)
        {
            lstList[i].style.height = parseInt(resHeight / lstList.length) + "px";
            //lstList[i].resetFlow();
        }
        for (var i = 0; i < m_arrList.length; i++)
        {
            m_arrList[i].data.elemNode.style.height = parseInt(resHeight / lstList.length) + "px";
            if (m_arrList[i].data.elemNode.style.display != "none" 
                && m_arrList[i].arrCol.length > 0)
            {
                m_arrList[i].resetFlow();
            }
        }
    }
    for (var i = 0; i < m_arrTab.length; i++)
    {
        var elemBar = $("Bar_" + m_arrTab[i].id.split("_")[1]);
        elemBar.style.top = getPosition(m_arrTab[i]).y + "px";
        elemBar.style.left = m_arrTab[i].offsetWidth - 80 + "px"; 
    }
}

//创建列表视图
function createListView(elemView, xmlView)
{
    for (var i = 0; i < xmlView.childNodes.length; i++)
    {
        var xmlNode = xmlView.childNodes[i];
        if ($XML(xmlNode, "kind") == AssUiNodeKind.Tab)
        {
            var elemNode = document.createElement("div");
            elemNode.id = "Tab_" + $XML(xmlNode, "id0");
            elemNode.className = "head_td_back";
            elemView.appendChild(elemNode);
            createBarDiv(elemNode, xmlNode);//创建展开关闭按钮
            var objTab = new CtnTabCtrl();
            objTab.create(elemNode.id);
            objTab.onTab = onTab;
            createTabAndList(elemNode, xmlNode, objTab);
            m_arrTab.push(elemNode);
        }
    }
}
function createBarDiv(elemNode,xmlNode)
{
   var elem = document.createElement("div");
   elem.id = "Bar_" + $XML(xmlNode, "id0");
   elem.style.position = "absolute";
//   elem.style.zIndex = "200";
//   elem.style.width = getPosition(lstTab[i]).y + "px";
//   elem.style.height = lstTab[i].offsetWidth - 80 + "px";
    elem.onclick = clickBar;
   elem.innerHTML = '<span><img src="../Images/tree/elbow-end-minus-nl.gif"/>收拢</span><span><img src="../Images/tree/elbow-plus-nl.gif"/>展开</span>';
   document.body.appendChild(elem);
   $(elem.lastChild).hide();
}
function clickBar()
{
    $(this.firstChild).toggle();
    $(this.lastChild).toggle();
    var ListView = $("Tab_" + this.id.split("_")[1]).nextSibling;
//    while (elemList)
//    {
//        if (this.firstChild.style.display == "none")
//        {
//            $(elemList).hide();
//        }
//        else
//        {
//            $(elemList).show();
//        }
//        elemList = elemList.nextSibling;
//    }
    $(ListView).toggle();
    initFrame();
}
//创建树视图function createTreeBarView(elemView, xmlView)
{
    
}

//创建按钮视图
function createButtonView(objList)
{
    elemView = m_btnView;
    xmlView  = objList.data.xmlNode;
    elemView.innerHTML = "";
    for (var i = 0; i < xmlView.childNodes.length; i++)
    {
        var xmlNode = xmlView.childNodes[i];
        if ($XML(xmlNode, "kind") == AssUiNodeKind.Button)
        {
            var elemNode = document.createElement("input");
            elemNode.type = "button";
            elemNode.className = "button";
            elemNode.style.width = "80px";
            elemNode.style.margin = "1px 2px";
            elemNode.value = $XML(xmlNode, "name");
            elemNode.idx = $XML(xmlNode, "idx");
            elemNode.xmlNode = xmlNode;
            elemNode.onclick = function (evt){
                AssExeTool(parseInt(this.idx), this.xmlNode);
            }
            elemView.appendChild(elemNode);
//            var textNode = document.createElement("span");
//            textNode.innerHTML = "&nbsp;&nbsp;";
//            elemView.appendChild(textNode);
        }
    }
}

//创建统计视图
function createStaticEditView(elemView, xmlView)
{
    for (var i = 0; i < xmlView.childNodes.length; i++)
    {
        var xmlNode = xmlView.childNodes[i];
        if ($XML(xmlNode, "kind") == AssUiNodeKind.Static)
        {
            var elemNode = document.createElement("span");
            elemNode.innerHTML = $XML(xmlNode, "name");
            elemView.appendChild(elemNode);
        }
        else if ($XML(xmlNode, "kind") == AssUiNodeKind.Edit)
        {
            var elemNode = document.createElement("input");
            elemNode.type = "text";
            elemNode.size = "4";
            elemNode.disabled = true;
            elemNode.value = $XML(xmlNode, "name");
            elemView.appendChild(elemNode);
        }
        else
        {
            continue;
        }
    }
}

function createTabAndList(elemTab, xmlTab, objTab)
{
    var ListView = document.createElement("div");
    ListView.style.overflow = "hidden";
    ListView.style.width = "100%";
    elemTab.parentNode.appendChild(ListView);
    for (var i = 0; i < xmlTab.childNodes.length; i++)
    {
        var xmlNode = xmlTab.childNodes[i];
        var attStyle = parseInt($XML(xmlNode, "style"));
        if ($XML(xmlNode, "kind") == AssUiNodeKind.List)
        {
            //创建列表
            var elemNode = document.createElement("div");
            elemNode.id = "LS_" + $XML(xmlNode, "id0");
            elemNode.style.overflow = "auto";
            elemNode.style.width = "100%";
            elemNode.style.height = "80px";//elemTab.parentNode.offsetHeight - 20 + "px";
            ListView.appendChild(elemNode);
            var idx = m_arrList.length;
            
            //创建列表
            m_arrList[idx] = new CtnListCtrl("m_arrList[" + idx + "]");
            m_arrList[idx].create(elemNode.id);
            m_arrList[idx].onRowClick = null;
            m_arrList[idx].bpage = 1;
            m_arrList[idx].bfill = 1;
            m_arrList[idx].bcheck = 1;
            m_arrList[idx].pagesize = 16;//getRowCount();
            m_arrList[idx].onRowDblClick = onRowDblClick;
            m_arrList[idx].data = {xmlNode:xmlNode};//存储XML节点
            m_arrList[idx].data.elemNode = elemNode;
            
            //如果与工程或局站有所关联的，则
            m_arrList[idx].data.attStyle = attStyle;//存储样式
            //向导时
            if (m_idx == m_vt.ASS_MAIN)
            {
                if (attStyle == AssUiAttStyle.Prj)
                {
                    //m_arrList[idx].bscheck = 1;
                    m_arrList[idx].data.strSql = m_bmlx + "='" + m_bm + "'";
                }
                else
                {
                    m_arrList[idx].data.strSql = "1<>1";
                }
            }
            else
            {
                if (m_bmlx == "GCBH" && attStyle == AssUiAttStyle.Prj)
                {
                    m_arrList[idx].data.strSql = "BM='" + m_bm + "'";
                }
                else if (m_bmlx != null &&
                (attStyle == AssUiAttStyle.Fkp
                || attStyle == AssUiAttStyle.Zkp
                || attStyle == AssUiAttStyle.Prj_Fkp
                || attStyle == AssUiAttStyle.Ext_Fkp
                ))
                {
                    //存储Sql条件
                    m_arrList[idx].data.strSql = m_bmlx + "='" + m_bm + "'";
                }
                else
                {
                    m_arrList[idx].data.strSql = "1=1";
                }
            }
            m_arrList[idx].onGoPage = onGoPage;
            
            objTab.append($XML(xmlNode, "name"), m_arrList[idx], elemNode.id);
            elemNode.idx = idx;
            elemNode.onclick = function(evt){
                onTab(m_arrList[this.idx]);
            };//点击页面按钮跟着变
        }
    }
    //选择初始列表
    if (xmlTab.childNodes.length > 0)
    {
        objTab.setActive(0);
    }
}


//获取头部XML
function GetColXml(xmlList)
{
    var xmlDoc = xmlList.data.xmlHead;
    if (!xmlDoc)
    {
        xmlDoc = CtnTrans.getXml("/main.ashx?_method=ASS_GetHeadXml", "post", "id0=" + $XML(xmlList.data.xmlNode, "id0"));
        if (!CtnError.validate(xmlDoc))
        {
            CtnError.show();
            return false;
        }
        xmlList.data.xmlHead = xmlDoc;
    }
    return xmlDoc;
}


function onGoPage(page)
{
    CtnTrans.Ajax("/main.ashx?_method=GetAssList", "POST", "sql=" + encodeURI(this.data.strSql) + "&id0=" + $XML(this.data.xmlNode,"id0") + "&page=" + page + "&pagesize=" + this.pagesize, showList, this,{loading:true});
}

function showList(res, objList)
{
    if (!CtnError.validate(res.responseText))
    {
        CtnError.show();
        return;
    }
    objList.reset();
    //取出的数据与uicfg中的头部合并
    var bodyXmlDoc = CtnXML.getXmlDoc();
    bodyXmlDoc.loadXML(res.responseText);
    var headXmlDoc = GetColXml(objList);
    if (!headXmlDoc)
    {
        return;
    }
    var bodyRoot = CtnXML.getRootNode(bodyXmlDoc);
    var headRoot = CtnXML.getRootNode(headXmlDoc);

    for (var i = 0; i < bodyRoot.attributes.length; i++)
    {
        var objNewAtt = headXmlDoc.createAttribute(bodyRoot.attributes[i].name);
        objNewAtt.value= bodyRoot.attributes[i].value;
        headRoot.attributes.setNamedItem(objNewAtt);
    }
    while (bodyRoot.childNodes.length > 0)
    {
        headRoot.appendChild(bodyRoot.childNodes[0]);//合并XML
    }
    objList.insertNode(headRoot, 1);
    objList.show();
    while (headRoot.childNodes.length > 1)
    {
        headRoot.removeChild(headRoot.lastChild);//合并XML
    }
}


//////////流程向导/////////
function initAssGuide()
{
    
}

function isGuide(idx)
{
    switch(idx)
    {
        case m_vt.ASS_XD_ZCCF:
        case m_vt.ASS_XD_CFQR:
        case m_vt.ASS_XD_ZCHB:
        case m_vt.ASS_XD_HBQR:
        case m_vt.ASS_XD_ZCJT:
        case m_vt.ASS_XD_JZJT:
        case m_vt.ASS_XD_JTQR:
        case m_vt.ASS_XD_KPDC:return true;
        default:return false;
    }
}

function getList(style)
{
    for (var i = 0; i < m_arrList.length; i++)
    {
        
    }
}
//双击列表
function onRowDblClick(evt, rowno)
{
    var attStyle = this.data.attStyle;
    if (attStyle == AssUiAttStyle.Prj)
    {
        for (var i = 0; i < m_arrList.length; i++)
        {
            var style = m_arrList[i].data.attStyle;
            if (style == AssUiAttStyle.Fkp
                ||style == AssUiAttStyle.Zkp
                ||style == AssUiAttStyle.Prj_Fkp
                ||style == AssUiAttStyle.Ext_Fkp)
            {
                m_arrList[i].data.strSql = "GCBH='" + this.getRow(rowno).getCell("BM").text + "'";
                m_arrList[i].refresh(1);
            }
        }
    }
    else if (attStyle == AssUiAttStyle.Fkp
            ||attStyle == AssUiAttStyle.Prj_Fkp
            ||attStyle == AssUiAttStyle.Ext_Fkp)
    {
        for (var i = 0; i < m_arrList.length; i++)
        {
            if (m_arrList[i].data.attStyle == AssUiAttStyle.Fkp_Chg)
            {
                m_arrList[i].data.strSql = "glid='" + this.getRow(rowno).getCell("ID0").text + "'";
                m_arrList[i].refresh(1);
            }
            else if (m_arrList[i].data.attStyle == AssUiAttStyle.Zkp)
            {
                m_arrList[i].data.strSql = "FKPXH='" + this.getRow(rowno).getCell("ID0").text + "'";
                m_arrList[i].refresh(1);
            }
        }
    }
    else if (attStyle == AssUiAttStyle.Zkp)
    {
        for (var i = 0; i < m_arrList.length; i++)
        {
            if (m_arrList[i].data.attStyle == AssUiAttStyle.Zkp_Chg)
            {
                m_arrList[i].data.strSql = "glid='" + this.getRow(rowno).getCell("ID0").text + "'";
                m_arrList[i].refresh(1);
            }
        }
    }
}
//获取元素位置
Number.prototype.NaN0=function(){return isNaN(this)?0:this;}
function getPosition(e)
{
    var left = 0;
    var top  = 0;
    while (e.offsetParent)
    {
        left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
        top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
        e     = e.offsetParent;
    }
    left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
    top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
    return {x:left, y:top};
}